<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 引入样式 -->
    <script src="../../js/vue.js"></script>
    <script src="../../js/axios.min.js"></script>
    <script src="../../js/dayjs.min.js"></script>
    <link rel="stylesheet" href="../../plugins/element_UI/index.css">
    <script src="../../plugins/element_UI/index.js"></script>
</head>
<body>
<div id="app">
    <el-row class="el-search-row-d" style="margin-right: auto">
        <el-input
                placeholder="请输入内容"
                v-model="dicName"
                style="width: fit-content"
                @change="init"
                clearable>
        </el-input>
        <el-button size="small" type="primary" @click="init">检索</el-button>
<!--        <el-button type="small" @click="dialogFormVisible = true">新建科室</el-button>-->
    </el-row>
    <el-dialog title="添加科室" :visible.sync="dialogFormVisible" >
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="科室编码" prop="depInternalCode">
                <el-input v-model="ruleForm.depInternalCode"></el-input>
            </el-form-item>
            <el-form-item label="科室名称" prop="depName">
                <el-input v-model="ruleForm.depName"></el-input>
            </el-form-item>
            <el-form-item label="科室别名" prop="depAlias">
                <el-input v-model="ruleForm.depAlias"></el-input>
            </el-form-item>
            <el-form-item label="父类科室" prop="depFatherId">
                <el-select v-model="ruleForm.depFatherId" placeholder="请选择父类科室">
                    <el-option label="内科" value="0010"></el-option>
                    <el-option label="骨科" value="0011"></el-option>
                    <el-option label="儿科" value="0012"></el-option>
                    <el-option label="妇科" value="0013"></el-option>
                    <el-option label="消化科" value="0014"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="活动形式" prop="desc">
                <el-input type="textarea" v-model="ruleForm.desc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                @current-change="handleCurrentChange"
                max-height="800"
                row-key="id"
                border
                :tree-props="{children: 'dictionaryList', hasChildren: 'hasChildren'}">
            <el-table-column
                    fixed
                    prop="dicName"
                    label="字典名称"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="dicCode"
                    label="字典代码"
                    width="120">
            </el-table-column>

            <el-table-column
                    prop="dicType"
                    label="字典类型"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="chinaSpell"
                    label="拼音码"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="dicSummary"
                    label="字典备注"
                    width="250">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-button
                            @click.native.prevent="deleteRow(scope.$index,scope.row)"
                            type="text"
                            disabled
                            size="small">
                        移除
                    </el-button>
                    <el-button
                            @click.native.prevent="editRow(scope.$index,scope.row)"
                            type="text"
                            disabled
                            size="small">
                        修改
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>
</div>
</body>
<script>
    let app = new Vue({
        el: '#app',
        data() {
            return {
                tableData: [],
                dicName: "",
                status: 0,
                loading: false,
                currentUpRow: null,

                ruleForm: {
                    chinaSpell: " ",
                    depAlias: "",
                    depCategory: "1",
                    depClinic: "1",
                    depFatherId: null,
                    depInternalCode: "",
                    depLevel: null,
                    depName: "",
                    depRegistration: "1",
                    depUpstage: "0",
                    description: "",
                    fiveStroke: "",
                    id: null,
                    status: "0"
                },
                rules: {
                    depName: [
                        { required: true, message: '请输入科室名称', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    depFatherId: [
                        { required: true, message: '请选择父类科室名称', trigger: 'change' }
                    ]
                },
                dialogTableVisible: false,
                dialogFormVisible: false,
                formLabelWidth: '120px'
            }
        },
        methods: {
            init: function () {
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                let that = this
                that.loading=false
                axios.get("http://"+window.location.host+'/dictionary/selectList', {
                    params: {dicName: this.dicName}
                }).then(res => {
                    that.tableData = res.data;
                    loading.close()
                }).catch((e) => {
                    alert('获取数据失败');
                    loading.close()
                });
            },
            handleCurrentChange(val) {
                this.currentUpRow = val;
                console.log(this.currentUpRow)
            },
            submitForm: function(ruleForm){
                this.$refs[ruleForm].validate((valid) => {
                    if (valid) {
                        axios.post("http://"+window.location.host+'/departments/create', this.ruleForm).then(res => {
                            alert("创建成功!");
                            this.$refs[ruleForm].resetFields();
                            this.dialogFormVisible = false
                        }).catch(function (error) {
                            alert(error.response.data.message)

                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            createDepartment: function(){
                let params = new URLSearchParams();
                params.append('startDate', startDate);
                params.append('endDate', endDate);
                axios.post("http://"+window.location.host+'/departments/selectList', params).then(res => {
                    if (res.status == 500 || res.status == '500'){
                        alert(res.message);
                    }
                    this.init()
                }).catch(function (error) {
                    loading.close();
                    alert(error.response.data.message)
                });
            },
            deleteRow: function(index, row) {
                let params = new URLSearchParams()
                params.append("id", row.id)
                console.log(row.id);
                axios.post("http://"+window.location.host+'/departments/delete',
                    params
                ).then(res => {
                    alert("删除成功！");
                    this.init()
                    this.tableData.splice(index, 1);
                })
            },
            editRow:function(index, row) {
                this.ruleForm=row;
                this.dialogFormVisible = true
            }
        },
        // watch: {
        //     datePickerArray(newValue, oldValue) {
        //         this.init();
        //     }
        // },
        created() {
            this.init()
        },
        mounted() {
        }
    });
</script>

</html>